package com.study.it.xw.controller.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//拦截器
//拦截器需要继承这个接口
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request
    , HttpServletResponse response,Object handler)throws Exception{
        //如果session中没有user的key值，说明从来没有登录过，就跳到login页
        if(request.getSession().getAttribute("user")==null){
            //跳转重定向的原始写法，动态获得工程名称/xw，动态获取为了通透性
            response.sendRedirect(request.getContextPath()+"/login");
            return false;
        }

        return true;

    }
}
